package org.loofer.ext.component.drawable;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import org.loofer.ext.component.drawable.DrawableDecorator.DrawableDecoratorState;

public class RoundCornerDrawable extends DrawableDecorator {
    private static final int DEFAULT_PAINT_FLAGS = 6;
    private static final ThreadLocal<Paint> sLocalPaint = new ThreadLocal<Paint>() {
        protected Paint initialValue() {
            Paint paint = new Paint(6);
            paint.setAntiAlias(true);
            return paint;
        }
    };
    private Path mPath;
    private RoundCorner mRoundCorner;
    private RoundCornerState mState;
    private RectF mTmpRect;

    static abstract class RoundCorner {
        public abstract void boundsChanged(Rect rect);

        public abstract boolean isValid();

        public abstract void prepareDraw(Paint paint);

        RoundCorner() {
        }
    }

    static class RoundCornerBitmap extends RoundCorner {
        private final int mBitmapHeight;
        private final int mBitmapWidth;
        private final int mGravity;
        private Matrix mMatrix;
        private final boolean mNoTile;
        private final Shader mShader;
        private final TileMode mTileModeX;
        private final TileMode mTileModeY;

        public RoundCornerBitmap(BitmapDrawable bitmapDrawable) {
            TileMode tileMode;
            int i = -1;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            TileMode tileModeX = bitmapDrawable.getTileModeX();
            TileMode tileModeY = bitmapDrawable.getTileModeY();
            this.mTileModeX = tileModeX == null ? TileMode.CLAMP : tileModeX;
            if (tileModeY == null) {
                tileMode = TileMode.CLAMP;
            } else {
                tileMode = tileModeY;
            }
            this.mTileModeY = tileMode;
            boolean z = tileModeX == null && tileModeY == null;
            this.mNoTile = z;
            this.mShader = bitmap == null ? null : new BitmapShader(bitmap, this.mTileModeX, this.mTileModeY);
            this.mGravity = bitmapDrawable.getGravity();
            this.mBitmapWidth = bitmap == null ? -1 : bitmap.getWidth();
            if (bitmap != null) {
                i = bitmap.getHeight();
            }
            this.mBitmapHeight = i;
        }

        public void prepareDraw(Paint paint) {
            paint.setShader(this.mShader);
        }

        public void boundsChanged(Rect bounds) {
            if (this.mShader != null && this.mGravity == 119 && this.mNoTile) {
                int width = bounds.width();
                int height = bounds.height();
                float widthScale = this.mBitmapWidth <= 0 ? 1.0f : ((float) width) / ((float) this.mBitmapWidth);
                float heightScale = this.mBitmapHeight <= 0 ? 1.0f : ((float) height) / ((float) this.mBitmapHeight);
                if (this.mMatrix == null) {
                    this.mMatrix = new Matrix();
                }
                this.mMatrix.reset();
                this.mMatrix.setScale(widthScale, heightScale);
                this.mShader.setLocalMatrix(this.mMatrix);
            }
        }

        public boolean isValid() {
            return this.mShader != null;
        }
    }

    static class RoundCornerState extends DrawableDecoratorState {
        boolean mIsOval;
        float mRadius;
        float[] mRadiusArray;
        boolean mUseArray;

        RoundCornerState(RoundCornerState orig, DrawableDecorator owner, Resources res) {
            super(orig, owner, res);
            if (orig != null) {
                this.mRadius = orig.mRadius;
                this.mRadiusArray = newArray(orig.mRadiusArray);
                this.mUseArray = orig.mUseArray;
                this.mIsOval = orig.mIsOval;
            }
        }

        public Drawable newDrawable() {
            return new RoundCornerDrawable(this, null);
        }

        public Drawable newDrawable(Resources res) {
            return new RoundCornerDrawable(this, res);
        }

        private static float[] newArray(float[] copy) {
            if (copy == null) {
                return null;
            }
            int length = copy.length;
            float[] array = new float[length];
            if (length <= 0) {
                return array;
            }
            System.arraycopy(copy, 0, array, 0, length);
            return array;
        }
    }

    public RoundCornerDrawable(Drawable drawable) {
        this(drawable, 0.0f);
    }

    public RoundCornerDrawable(Drawable drawable, float radius) {
        this.mPath = new Path();
        this.mTmpRect = new RectF();
        this.mState = new RoundCornerState(null, this, null);
        this.mState.setDrawable(drawable);
        setConstantState(this.mState);
        setRadius(radius);
        init(drawable);
    }

    public RoundCornerDrawable(Drawable drawable, float[] radiusArray) {
        this.mPath = new Path();
        this.mTmpRect = new RectF();
        this.mState = new RoundCornerState(null, this, null);
        this.mState.setDrawable(drawable);
        setConstantState(this.mState);
        setRadius(radiusArray);
        init(drawable);
    }

    private void init(Drawable drawable) {
        if (drawable != null && (drawable instanceof BitmapDrawable)) {
            this.mRoundCorner = new RoundCornerBitmap((BitmapDrawable) drawable);
        }
    }

    public void setRadius(float radius) {
        this.mState.mUseArray = false;
        if (this.mState.mRadius != radius) {
            this.mState.mRadius = radius;
            invalidateSelf();
        }
    }

    public void setRadius(float[] radiusArray) {
        if (radiusArray == null || radiusArray.length >= 8) {
            this.mState.mUseArray = true;
            this.mState.mRadiusArray = radiusArray;
            invalidateSelf();
            return;
        }
        throw new ArrayIndexOutOfBoundsException("radius array must have >= 8 values");
    }

    public void setOval(boolean isOval) {
        if (this.mState.mIsOval != isOval) {
            this.mState.mIsOval = isOval;
            invalidateSelf();
        }
    }

    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        if (this.mRoundCorner != null && this.mRoundCorner.isValid()) {
            this.mRoundCorner.boundsChanged(bounds);
        }
    }

    public void draw(Canvas canvas) {
        boolean isOval = this.mState.mIsOval;
        float radius = this.mState.mRadius;
        float[] radiusArray = this.mState.mRadiusArray;
        if (!isOval && radius == 0.0f && radiusArray == null) {
            super.draw(canvas);
        } else if (this.mRoundCorner == null || !this.mRoundCorner.isValid()) {
            path = this.mPath;
            rect = this.mTmpRect;
            rect.set(getBounds());
            path.reset();
            if (isOval) {
                path.addOval(rect, Direction.CW);
            } else if (this.mState.mUseArray) {
                path.addRoundRect(rect, radiusArray, Direction.CW);
            } else {
                path.addRoundRect(rect, radius, radius, Direction.CW);
            }
            int saveCount = canvas.save();
            try {
                canvas.clipPath(path);
                super.draw(canvas);
            } catch (UnsupportedOperationException e) {
            } finally {
                canvas.restoreToCount(saveCount);
            }
        } else {
            Paint paint = (Paint) sLocalPaint.get();
            path = this.mPath;
            rect = this.mTmpRect;
            rect.set(getBounds());
            paint.setShader(null);
            this.mRoundCorner.prepareDraw(paint);
            if (isOval) {
                canvas.drawOval(rect, paint);
            } else if (this.mState.mUseArray) {
                path.reset();
                path.addRoundRect(rect, radiusArray, Direction.CW);
                canvas.drawPath(path, paint);
            } else {
                canvas.drawRoundRect(rect, radius, radius, paint);
            }
        }
    }

    private RoundCornerDrawable(RoundCornerState state, Resources res) {
        this.mPath = new Path();
        this.mTmpRect = new RectF();
        this.mState = new RoundCornerState(state, this, res);
        setConstantState(this.mState);
    }
}
